var app = getApp();
Component({
    properties: {
        isPush : Boolean,
        info : Object,
        params : Object,
    },
    data: {
        isshow : true,
        isMirror : false,
        isSD : false,
        isBeauty : false,
        isFront : false,
        isMirror : false,
        isauth : true, // 需要先授权
        isshowmore : false,
        issxt : false,
        gift : false,
        ising : false,
        isjx :false,
        authtype : 1,
        showprize : false,
        prize : {name:'',type:'0',times:10,num:10,prizetype:'1',isstart:'1'},
        beauty : 60,
        whiteness : 30,
    },
    options : {
        addGlobalClass : true,
    },
    ready: function() {
        var self = this;
        this.setData({
            isSD : this.data.info.hd*1 == 1 ? true : false,
            isFront : this.data.info.isfront*1 == 1 ? true : false,
            isMirror : this.data.info.mirror*1 == 1 ? true : false,
        })
    },
    methods: {
        navigateto : function( e ){
            app.com.navigateto( e );
        },
        onClose : function( e ){
            this.setData({
                isshow : !this.data.isshow,
                isBeauty : false,
            })
        },
        onClosemore(){
            this.setData({
                isshowmore : !this.data.isshowmore,
                isBeauty : false,
            })
        },
        switchDefinition(){
            this.setData({
                isSD : !this.data.isSD
            })
            this.triggerEvent('switch',{type:'isSD',value:this.data.isSD});
            this.changelive('hd',this.data.isSD ? 1 : 0);
        },
        switchBeauty(){
            this.setData({
                isBeauty : true,
                isshowmore : false,
            })
        },
        hidebeaty(){
            this.setData({
                isBeauty : false,
            })

        },
        onChangebeaty(e){
            var type = e.currentTarget.dataset.type*1;
            if(type == 1){
                var whiteness = e.detail;
                this.data.whiteness = whiteness;

                var beauty = this.data.beauty;
            }else{
                var whiteness = this.data.whiteness;
                var beauty = e.detail;
                this.data.beauty = whiteness;
            }
            var data = {
                beauty : beauty,
                whiteness : whiteness,
            }
            this.triggerEvent('setbeauty',data);
        },
        switchCamera(){
            this.setData({
                isFront : !this.data.isFront
            })
            this.triggerEvent('switch',{type:'isFront',value:this.data.isFront});
            this.changelive('front',this.data.isFront ? 1 : 0);
        },
        switchCameraa(){
            this.setData({
                issxt : !this.data.issxt
            })
            this.triggerEvent('switch',{type:'issxt',value:this.data.issxt});
            app.com.toast('已切换');
        },
        switchMirror(){
            this.setData({
                isMirror : !this.data.isMirror
            })
            this.triggerEvent('switch',{type:'isMirror',value:this.data.isMirror});
            this.changelive('mirror',this.data.isMirror ? 1 : 0);
        },
        switchjx(){
            this.setData({
                isjx : !this.data.isjx
            })
            this.triggerEvent('switch',{type:'isjx',value:this.data.isjx});
            app.com.toast('已切换');
        },
        changelive(type,status){
            var self = this;
            var postdata = {
                op : 'changelive',
                type : type,
                status : status,
                liveid :  self.data.info.id,
            };
            app.com.http('live','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {

                    if(res.data.data.mess){
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                    }
                    
                    app.com.toast(res.data.message);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        },
        tellsub(){
            var self = this;
            var postdata = {
                op : 'tellsub',
                liveid :  self.data.info.id,
            };
            app.com.http('live','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    app.com.toast(res.data.message);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        },
        closelive(){
            var self = this;
            var postdata = {
                op : 'closelive',
                liveid :  self.data.info.id,
            };
            app.com.confirm('确定关闭直播吗？',function(){
                app.com.http('live','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        app.com.toast(res.data.message);
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                        self.triggerEvent('closelive',1);
                        self.setData({
                            isshowmore : false,
                        })
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            })
        },
        sendGo(content){
            var self = this;
            app.globalData.goEasy.publish({
                channel: 'live'+self.data.info.code,
                message: content
            });
        },
        onTouchLike(e){
            var self = this;
            self.data.authtype = 1;

            if(self.data.info.isauth == 1 && self.data.isauth){
                self.loginauth = this.selectComponent('#loginauth');
                self.loginauth.hideauth();
                return false;
            }

            self.setData({
                hoverLike: !0,
                dataLike: 1
            });
            var fn = function() {
                self.triggerEvent('showBubble',1);
            };
            fn();
            clearInterval(self.longTapBubbleTimer);
            self.longTapBubbleTimer = setInterval(fn, 150);
        },
        onCancelTouchLike: function() {
            clearInterval(this.longTapBubbleTimer);
            this.zanit();
        },
        authed(e){
            var self = this;
            self.setData({
                isauth : false,
            })

            if( self.data.authtype == 1 ){
                self.onTouchLike();
                setTimeout(function(){
                    self.onCancelTouchLike();
                },50)
            }else if(self.data.authtype == 2){
                self.showgift();
            }
        },
        zanit(){
            var self = this;
            var postdata = {
                op : 'zanlive',
                liveid :  self.data.info.id,
            };
            
            app.com.http('live','POST',postdata,0,false,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    if(res.data.data.mess){
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                    }
                    self.addzan();
                    app.com.toast(res.data.message);
                }else{
                    app.com.toast(res.data.message);
                }
            });            
        },
        addzan(e){
            this.setData({
                'info.zan' : this.data.info.zan*1+1
            })
        },
        showShareBar(){
            this.triggerEvent('shareit',1);
        },
        showgift(){
            this.data.authtype = 2;
            if(this.data.info.isauth == 1 && this.data.isauth){
                this.loginauth = this.selectComponent('#loginauth');
                this.loginauth.hideauth();
                return false;
            }
            this.triggerEvent('showgift',1);
        },
        pubprize(){
            this.setData({
                showprize : !this.data.showprize,
            })
        },
        subprize(){
            var self = this;
            var postdata = {
                op : 'addprize',
                liveid :  self.data.info.id,
                name : self.data.prize.name,
                num : self.data.prize.num,
                times : self.data.prize.times,
                prizetype : self.data.prize.prizetype,
                type : self.data.prize.type,
                isstart : self.data.prize.isstart,
            };
            app.com.confirm('发布后不可修改，确定发布吗？',function(){
                app.com.http('live','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        self.setData({
                            showprize : false,
                            isshowmore : false,
                        })

                        self.triggerEvent('newchou',res.data.data.info);

                        app.com.toast(res.data.message);
                    }else{
                        app.com.toast(res.data.message);
                    }
                }); 
            }); 
        },
        onChange(e){
            var type = e.currentTarget.dataset.type;
            this.setData({
                ['prize.'+type] : e.detail
            })
        }
    }

})